home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zdict.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.3 KB  |  505 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zdict.c,v 1.3 2000/09/19 19:00:53 lpd Exp $ */
  20. /* Dictionary operators */
  21. #include "ghost.h"
  22. #include "oper.h"
  23. #include "iddict.h"
  24. #include "dstack.h"
  25. #include "ilevel.h"        /* for [count]dictstack */
  26. #include "iname.h"        /* for dict_find_name */
  27. #include "ipacked.h"        /* for inline dict lookup */
  28. #include "ivmspace.h"
  29. #include "store.h"
  30.  
  31. /* <int> dict <dict> */
  32. int
  33. zdict(i_ctx_t *i_ctx_p)
  34. {
  35.     os_ptr op = osp;
  36.  
  37.     check_type(*op, t_integer);
  38. #if arch_sizeof_int < arch_sizeof_long
  39.     check_int_leu(*op, max_uint);
  40. #else
  41.     if (op->value.intval < 0)
  42.     return_error(e_rangecheck);
  43. #endif
  44.     return dict_create((uint) op->value.intval, op);
  45. }
  46.  
  47. /* <dict> maxlength <int> */
  48. private int
  49. zmaxlength(i_ctx_t *i_ctx_p)
  50. {
  51.     os_ptr op = osp;
  52.  
  53.     check_type(*op, t_dictionary);
  54.     check_dict_read(*op);
  55.     make_int(op, dict_maxlength(op));
  56.     return 0;
  57. }
  58.  
  59. /* <dict> begin - */
  60. int
  61. zbegin(i_ctx_t *i_ctx_p)
  62. {
  63.     os_ptr op = osp;
  64.  
  65.     check_type(*op, t_dictionary);
  66.     check_dict_read(*op);
  67.     if (dsp == dstop)
  68.     return_error(e_dictstackoverflow);
  69.     ++dsp;
  70.     ref_assign(dsp, op);
  71.     dict_set_top();
  72.     pop(1);
  73.     return 0;
  74. }
  75.  
  76. /* - end - */
  77. int
  78. zend(i_ctx_t *i_ctx_p)
  79. {
  80.     if (ref_stack_count_inline(&d_stack) == min_dstack_size) {
  81.     /* We would underflow the d-stack. */
  82.     return_error(e_dictstackunderflow);
  83.     }
  84.     while (dsp == dsbot) {
  85.     /* We would underflow the current block. */
  86.     ref_stack_pop_block(&d_stack);
  87.     }
  88.     dsp--;
  89.     dict_set_top();
  90.     return 0;
  91. }
  92.  
  93. /* <key> <value> def - */
  94. /*
  95.  * We make this into a separate procedure because
  96.  * the interpreter will almost always call it directly.
  97.  */
  98. int
  99. zop_def(i_ctx_t *i_ctx_p)
  100. {
  101.     os_ptr op = osp;
  102.     os_ptr op1 = op - 1;
  103.     ref *pvslot;
  104.  
  105.     /* The following combines a check_op(2) with a type check. */
  106.     switch (r_type(op1)) {
  107.     case t_name: {
  108.         /* We can use the fast single-probe lookup here. */
  109.         uint nidx = name_index(op1);
  110.         uint htemp;
  111.  
  112.         if_dict_find_name_by_index_top(nidx, htemp, pvslot) {
  113.         if (dtop_can_store(op))
  114.             goto ra;
  115.         }
  116.         break;        /* handle all slower cases */
  117.         }
  118.     case t_null:
  119.         return_error(e_typecheck);
  120.     case t__invalid:
  121.         return_error(e_stackunderflow);
  122.     }
  123.     /*
  124.      * Combine the check for a writable top dictionary with
  125.      * the global/local store check.  See dstack.h for details.
  126.      */
  127.     if (!dtop_can_store(op)) {
  128.     check_dict_write(*dsp);
  129.     /*
  130.      * If the dictionary is writable, the problem must be
  131.      * an invalid store.
  132.      */
  133.     return_error(e_invalidaccess);
  134.     }
  135.     /*
  136.      * Save a level of procedure call in the common (redefinition)
  137.      * case.  With the current interfaces, we pay a double lookup
  138.      * in the uncommon case.
  139.      */
  140.     if (dict_find(dsp, op1, &pvslot) <= 0)
  141.     return idict_put(dsp, op1, op);
  142. ra:
  143.     ref_assign_old_inline(&dsp->value.pdict->values, pvslot, op,
  144.               "dict_put(value)");
  145.     return 0;
  146. }
  147. int
  148. zdef(i_ctx_t *i_ctx_p)
  149. {
  150.     int code = zop_def(i_ctx_p);
  151.  
  152.     if (code >= 0) {
  153.     pop(2);
  154.     }
  155.     return code;
  156. }
  157.  
  158. /* <key> load <value> */
  159. private int
  160. zload(i_ctx_t *i_ctx_p)
  161. {
  162.     os_ptr op = osp;
  163.     ref *pvalue;
  164.  
  165.     switch (r_type(op)) {
  166.     case t_name:
  167.         /* Use the fast lookup. */
  168.         if ((pvalue = dict_find_name(op)) == 0)
  169.         return_error(e_undefined);
  170.         ref_assign(op, pvalue);
  171.         return 0;
  172.     case t_null:
  173.         return_error(e_typecheck);
  174.     case t__invalid:
  175.         return_error(e_stackunderflow);
  176.     default: {
  177.         /* Use an explicit loop. */
  178.         uint size = ref_stack_count(&d_stack);
  179.         uint i;
  180.  
  181.         for (i = 0; i < size; i++) {
  182.             ref *dp = ref_stack_index(&d_stack, i);
  183.  
  184.             check_dict_read(*dp);
  185.             if (dict_find(dp, op, &pvalue) > 0) {
  186.             ref_assign(op, pvalue);
  187.             return 0;
  188.             }
  189.         }
  190.         return_error(e_undefined);
  191.         }
  192.     }
  193. }
  194.  
  195. /* get - implemented in zgeneric.c */
  196.  
  197. /* put - implemented in zgeneric.c */
  198.  
  199. /* <dict> <key> .undef - */
  200. /* <dict> <key> undef - */
  201. private int
  202. zundef(i_ctx_t *i_ctx_p)
  203. {
  204.     os_ptr op = osp;
  205.  
  206.     check_type(op[-1], t_dictionary);
  207.     check_dict_write(op[-1]);
  208.     idict_undef(op - 1, op);    /* ignore undefined error */
  209.     pop(2);
  210.     return 0;
  211. }
  212.  
  213. /* <dict> <key> known <bool> */
  214. private int
  215. zknown(i_ctx_t *i_ctx_p)
  216. {
  217.     os_ptr op = osp;
  218.     register os_ptr op1 = op - 1;
  219.     ref *pvalue;
  220.  
  221.     check_type(*op1, t_dictionary);
  222.     check_dict_read(*op1);
  223.     make_bool(op1, (dict_find(op1, op, &pvalue) > 0 ? 1 : 0));
  224.     pop(1);
  225.     return 0;
  226. }
  227.  
  228. /* <key> where <dict> true */
  229. /* <key> where false */
  230. int
  231. zwhere(i_ctx_t *i_ctx_p)
  232. {
  233.     os_ptr op = osp;
  234.     ref_stack_enum_t rsenum;
  235.  
  236.     check_op(1);
  237.     ref_stack_enum_begin(&rsenum, &d_stack);
  238.     do {
  239.     const ref *const bot = rsenum.ptr;
  240.     const ref *pdref = bot + rsenum.size;
  241.     ref *pvalue;
  242.  
  243.     while (pdref-- > bot) {
  244.         check_dict_read(*pdref);
  245.         if (dict_find(pdref, op, &pvalue) > 0) {
  246.         push(1);
  247.         ref_assign(op - 1, pdref);
  248.         make_true(op);
  249.         return 0;
  250.         }
  251.     }
  252.     } while (ref_stack_enum_next(&rsenum));
  253.     make_false(op);
  254.     return 0;
  255. }
  256.  
  257. /* copy for dictionaries -- called from zcopy in zgeneric.c. */
  258. /* Only the type of *op has been checked. */
  259. int
  260. zcopy_dict(i_ctx_t *i_ctx_p)
  261. {
  262.     os_ptr op = osp;
  263.     os_ptr op1 = op - 1;
  264.     int code;
  265.  
  266.     check_type(*op1, t_dictionary);
  267.     check_dict_read(*op1);
  268.     check_dict_write(*op);
  269.     if (!dict_auto_expand &&
  270.     (dict_length(op) != 0 || dict_maxlength(op) < dict_length(op1))
  271.     )
  272.     return_error(e_rangecheck);
  273.     code = idict_copy(op1, op);
  274.     if (code < 0)
  275.     return code;
  276.     /*
  277.      * In Level 1 systems, we must copy the access attributes too.
  278.      * The only possible effect this can have is to make the
  279.      * copy read-only if the original dictionary is read-only.
  280.      */
  281.     if (!level2_enabled)
  282.     r_copy_attrs(dict_access_ref(op), a_write, dict_access_ref(op1));
  283.     ref_assign(op1, op);
  284.     pop(1);
  285.     return 0;
  286. }
  287.  
  288. /* - currentdict <dict> */
  289. private int
  290. zcurrentdict(i_ctx_t *i_ctx_p)
  291. {
  292.     os_ptr op = osp;
  293.  
  294.     push(1);
  295.     ref_assign(op, dsp);
  296.     return 0;
  297. }
  298.  
  299. /* - countdictstack <int> */
  300. private int
  301. zcountdictstack(i_ctx_t *i_ctx_p)
  302. {
  303.     os_ptr op = osp;
  304.     uint count = ref_stack_count(&d_stack);
  305.  
  306.     push(1);
  307.     if (!level2_enabled)
  308.     count--;        /* see dstack.h */
  309.     make_int(op, count);
  310.     return 0;
  311. }
  312.  
  313. /* <array> dictstack <subarray> */
  314. private int
  315. zdictstack(i_ctx_t *i_ctx_p)
  316. {
  317.     os_ptr op = osp;
  318.     uint count = ref_stack_count(&d_stack);
  319.  
  320.     check_write_type(*op, t_array);
  321.     if (!level2_enabled)
  322.     count--;        /* see dstack.h */
  323.     return ref_stack_store(&d_stack, op, count, 0, 0, true, idmemory,
  324.                "dictstack");
  325. }
  326.  
  327. /* - cleardictstack - */
  328. private int
  329. zcleardictstack(i_ctx_t *i_ctx_p)
  330. {
  331.     while (zend(i_ctx_p) >= 0)
  332.     DO_NOTHING;
  333.     return 0;
  334. }
  335.  
  336. /* ------ Extensions ------ */
  337.  
  338. /* <dict1> <dict2> .dictcopynew <dict2> */
  339. private int
  340. zdictcopynew(i_ctx_t *i_ctx_p)
  341. {
  342.     os_ptr op = osp;
  343.     os_ptr op1 = op - 1;
  344.     int code;
  345.  
  346.     check_type(*op1, t_dictionary);
  347.     check_dict_read(*op1);
  348.     check_type(*op, t_dictionary);
  349.     check_dict_write(*op);
  350.     /* This is only recognized in Level 2 mode. */
  351.     if (!dict_auto_expand)
  352.     return_error(e_undefined);
  353.     code = idict_copy_new(op1, op);
  354.     if (code < 0)
  355.     return code;
  356.     ref_assign(op1, op);
  357.     pop(1);
  358.     return 0;
  359. }
  360.  
  361. /* -mark- <key0> <value0> <key1> <value1> ... .dicttomark <dict> */
  362. /* This is the Level 2 >> operator. */
  363. private int
  364. zdicttomark(i_ctx_t *i_ctx_p)
  365. {
  366.     uint count2 = ref_stack_counttomark(&o_stack);
  367.     ref rdict;
  368.     int code;
  369.     uint idx;
  370.  
  371.     if (count2 == 0)
  372.     return_error(e_unmatchedmark);
  373.     count2--;
  374.     if ((count2 & 1) != 0)
  375.     return_error(e_rangecheck);
  376.     code = dict_create(count2 >> 1, &rdict);
  377.     if (code < 0)
  378.     return code;
  379.     /* << /a 1 /a 2 >> => << /a 1 >>, i.e., */
  380.     /* we must enter the keys in top-to-bottom order. */
  381.     for (idx = 0; idx < count2; idx += 2) {
  382.     code = idict_put(&rdict,
  383.              ref_stack_index(&o_stack, idx + 1),
  384.              ref_stack_index(&o_stack, idx));
  385.     if (code < 0) {        /* There's no way to free the dictionary -- too bad. */
  386.         return code;
  387.     }
  388.     }
  389.     ref_stack_pop(&o_stack, count2);
  390.     ref_assign(osp, &rdict);
  391.     return code;
  392. }
  393.  
  394. /* <dict> <key> .forceundef - */
  395. /*
  396.  * This forces an "undef" even if the dictionary is not writable.
  397.  * Like .forceput, it is meant to be used only in a few special situations,
  398.  * and should not be accessible by name after initialization.
  399.  */
  400. private int
  401. zforceundef(i_ctx_t *i_ctx_p)
  402. {
  403.     os_ptr op = osp;
  404.  
  405.     check_type(op[-1], t_dictionary);
  406.     /* Don't check_dict_write */
  407.     idict_undef(op - 1, op);    /* ignore undefined error */
  408.     pop(2);
  409.     return 0;
  410. }
  411.  
  412. /* <dict> <key> .knownget <value> true */
  413. /* <dict> <key> .knownget false */
  414. private int
  415. zknownget(i_ctx_t *i_ctx_p)
  416. {
  417.     os_ptr op = osp;
  418.     register os_ptr op1 = op - 1;
  419.     ref *pvalue;
  420.  
  421.     check_type(*op1, t_dictionary);
  422.     check_dict_read(*op1);
  423.     if (dict_find(op1, op, &pvalue) <= 0) {
  424.     make_false(op1);
  425.     pop(1);
  426.     } else {
  427.     ref_assign(op1, pvalue);
  428.     make_true(op);
  429.     }
  430.     return 0;
  431. }
  432.  
  433. /* <dict> <key> .knownundef <bool> */
  434. private int
  435. zknownundef(i_ctx_t *i_ctx_p)
  436. {
  437.     os_ptr op = osp;
  438.     os_ptr op1 = op - 1;
  439.     int code;
  440.  
  441.     check_type(*op1, t_dictionary);
  442.     check_dict_write(*op1);
  443.     code = idict_undef(op1, op);
  444.     make_bool(op1, code == 0);
  445.     pop(1);
  446.     return 0;
  447. }
  448.  
  449. /* <dict> <int> .setmaxlength - */
  450. private int
  451. zsetmaxlength(i_ctx_t *i_ctx_p)
  452. {
  453.     os_ptr op = osp;
  454.     os_ptr op1 = op - 1;
  455.     uint new_size;
  456.     int code;
  457.  
  458.     check_type(*op1, t_dictionary);
  459.     check_dict_write(*op1);
  460.     check_type(*op, t_integer);
  461. #if arch_sizeof_int < arch_sizeof_long
  462.     check_int_leu(*op, max_uint);
  463. #else
  464.     if (op->value.intval < 0)
  465.     return_error(e_rangecheck);
  466. #endif
  467.     new_size = (uint) op->value.intval;
  468.     if (dict_length(op - 1) > new_size)
  469.     return_error(e_dictfull);
  470.     code = idict_resize(op - 1, new_size);
  471.     if (code >= 0)
  472.     pop(2);
  473.     return code;
  474. }
  475.  
  476. /* ------ Initialization procedure ------ */
  477.  
  478. /* We need to split the table because of the 16-element limit. */
  479. const op_def zdict1_op_defs[] = {
  480.     {"0cleardictstack", zcleardictstack},
  481.     {"1begin", zbegin},
  482.     {"0countdictstack", zcountdictstack},
  483.     {"0currentdict", zcurrentdict},
  484.     {"2def", zdef},
  485.     {"1dict", zdict},
  486.     {"0dictstack", zdictstack},
  487.     {"0end", zend},
  488.     {"2known", zknown},
  489.     {"1load", zload},
  490.     {"1maxlength", zmaxlength},
  491.     {"2.undef", zundef},    /* we need this even in Level 1 */
  492.     {"1where", zwhere},
  493.     op_def_end(0)
  494. };
  495. const op_def zdict2_op_defs[] = {
  496.         /* Extensions */
  497.     {"2.dictcopynew", zdictcopynew},
  498.     {"1.dicttomark", zdicttomark},
  499.     {"2.forceundef", zforceundef},
  500.     {"2.knownget", zknownget},
  501.     {"1.knownundef", zknownundef},
  502.     {"2.setmaxlength", zsetmaxlength},
  503.     op_def_end(0)
  504. };
  505.